<template>
  <div class="table">
    <!-- header数据 -->
    <div class="header">
      <h3 class="title">{{ title }}</h3>
      <slot name="headerOperation"></slot>
    </div>
    <!-- table数据 -->
    <el-table
      :data="pageList"
      style="width: 100%"
      border
      @selection-change="handleSelectionChange"
      v-bind="expandProps"
    >
      <el-table-column v-if="showSelectColumn" type="selection" align="center" width="60"></el-table-column>
      <el-table-column v-if="showIndexColumn" type="index" label="序号" align="center" width="60"></el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <template v-slot="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- footer数据 -->
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          :currentPage="pageInfo.currentPage"
          :page-size="pageInfo.currentSize"
          :page-sizes="[3, 10, 15]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    pageList: {
      type: Array,
      required: true
    },
    pageCount: {
      type: Number,
      default: 0
    },
    pageInfo: {
      type: Object,
      default: () => ({ currentPage: 0, currentSize: 10 })
    },
    title: {
      type: String
    },
    propList: {
      type: Array,
      default: () => []
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    expandProps: {
      type: Object,
      default: () => ({})
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  emits: ['selectionChange', 'update:pageInfo'],
  setup(props, { emit }) {
    const handleSelectionChange = (value: any) => {
      emit('selectionChange', value)
    }

    const handleSizeChange = (currentSize: number) => {
      emit('update:pageInfo', { ...props.pageInfo, currentSize })
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:pageInfo', { ...props.pageInfo, currentPage })
    }

    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped lang="less">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.footer {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
